# %%
from DataTools import datatool
from DataGeter import parse_url
from OtherTools import filetool
from tqdm import tqdm
import pandas as pd
import gc
import os
import random
import time
import re
from rich import print
# %%
def getReports(code, mode=0):
    """
    + 获取所有财务信息163
    + 反馈url
    + getReports(SercherCode(inputstr="600009", methods=0))
    + mode = 0
    """
    urllist = []
    ascessurl = "http://quotes.money.163.com/service/"

    if mode == 0 or mode == 1:
        typelists = [
            ".html?type=report",  # cwbb = "http://quotes.money.163.com/service/zycwzb_{}.html?type=report".format(code) # 主要财务数据
            ".html?type=report&part=ylnl",  # ylnl = "http://quotes.money.163.com/service/zycwzb_{}.html?type=report&part=ylnl".format(code) # 盈利能力
            ".html?type=report&part=chnl",  # cznl = "http://quotes.money.163.com/service/zycwzb_{}.html?type=report&part=cznl".format(code) # 成长能力
            ".html?type=report&part=cznl",  # chnl = "http://quotes.money.163.com/service/zycwzb_{}.html?type=report&part=chnl".format(code) # 还债能力
            ".html?type=report&part=yynl",  # yynl = "http://quotes.money.163.com/service/zycwzb_{}.html?type=report&part=yynl".format(code) # 营运能力
        ]
        for key in typelists:
            f = ascessurl + "zycwzb_" + code + key
            urllist.append(f)

    if mode == 0 or mode == 2:
        typelists = ["lrb_", "zcfzb_", "xjllb_", "cwbbzy_"]
        # lrb = "http://quotes.money.163.com/service/lrb_{}.html".format(code) # 利润表
        # cwbbzy = "http://quotes.money.163.com/service/cwbbzy_{}.html".format(code) # 财务报告
        # zcfzb = "http://quotes.money.163.com/service/zcfzb_{}.html".format(code) # 资产负债表
        # xjllb = "http://quotes.money.163.com/service/xjllb_{}.html".format(code) #现金流量表
        for key in typelists:
            f = ascessurl + key + code + ".html"
            urllist.append(f)
    return urllist


def GetOutcome(url):
    """
    request: url from getReports(code, mode=0)
    """
    header = {
        "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36",
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    }
    try:
        outcome = parse_url._parse_url(url, headers=header, encodeing="GBK")
    except Exception as e:
        print(e)
        outcome = None
    return outcome


def retryLoop(url, retrytime, totaltime):

    outcome = ""
    kill = 0

    for i in range(retrytime):  # 重试次数

        outcome = GetOutcome(url)

        if outcome == "" or outcome == None:  # 表格不含内容，重试（request返回 None）
            timmer = parse_url.sleeper(50, 300, 100)  # 平均请求时间大于5秒不容易被封
            kill += 1
            print(
                "retrying >>> {}/{} >>> {}".format(kill, retrytime, timmer).center(
                    80, " "
                )
            )
            totaltime += timmer
            if kill == retrytime:  # 重试超过5次退出程序
                exit()

        else:  # 函数有返回值时进行保存（空值会报错）
            kill = 0
            return outcome, totaltime


def GetlastReportDate(con):
    re_lists = re.findall("(\d\d\d\d)-(\d\d)-(\d\d),", con)
    time_list = []
    for i in re_lists:
        time_list.append("-".join(i))
    time_list.sort()
    return time_list[-1]


def FancMainLoop(mode=2, MainPath=r"D:/StockDatas/", subPath=r"Reports/", retrytime=5):
    """
    + 通过163获取
    """
    printLens = 60

    count = 0
    totaltime = 0
    codelist = datatool.getCodeAndDetail(MainPath=MainPath, keylist=["ts_code"])
    codelist = set(codelist.keys())
    totalCodes = len(codelist)
    path = MainPath + subPath
    lastReportDate = ""

    for code in codelist:  # 进行随机抽取
        print("\n" * 3)
        print(code.center(printLens, " "))

        urlList = getReports(code, mode)
        for url in urlList:
            count += 1
            index = urlList.index(url)

            filename = "{}_{}_{}.csv".format(index, code, lastReportDate)
            Fp = path + filename

            if filetool.FileCheck(Fp):
                print("{} nothing to update".format(Fp).center(printLens, "="))

            else:
                print(url.center(printLens, " "))
                outcome, totaltime = retryLoop(
                    url,
                    retrytime,
                    totaltime,
                )

                lastReportDate = GetlastReportDate(outcome)  # 通过RE模块获取最后一次更新时间

                filename = "{}_{}_{}.csv".format(index, code, lastReportDate)
                Fp = path + filename

                with open(Fp, "w") as f:
                    f.write(outcome)

                timmer = parse_url.sleeper(10, 100, 100)
                totaltime += timmer
                print(Fp.center(printLens, " "))
                print("\n")
        print(
            "{}/{} | {} Mins | {} Hor  ".format(
                count,
                totalCodes * len(urlList),
                round(totaltime / 60, 2),
                round(
                    totaltime / (count / totalCodes / len(urlList)) / (60 * 60),
                    2,
                ),
            ).center(printLens, " "),
        )
        dirl = os.listdir(path)

        for dirs in dirl:
            if code in dirs:
                st = filename
                st = st.split("_")
                st = st[-1]
                if dirs.endswith(st):
                    pass
                else:
                    di = path + dirs
                    print("{}==>  removed {} not meching".format(di, st))
                    os.remove(di)
            else:
                print("not matching")
                break


def FianceReportMain(
    mode=2, MainPath=r"D:/StockDatas/", subPath=r"Reports/", retrytime=5
):
    path = MainPath + subPath
    filetool.DirPathChick(path)
    FancMainLoop(mode=mode, MainPath=MainPath, subPath=subPath, retrytime=retrytime)
    # filetool.AutoRmove(path, filename)

    print("done".center(80, "*"))
    gc.collect()


if __name__ == "__main__":
    FianceReportMain(
        mode=2, MainPath=r"D:/StockDatas/", subPath=r"Reports/", retrytime=5
    )
    exit()

# %%


# %%
